package com.lkm.app_ui.m.Imple.initapp;

import android.content.Context;
import android.content.Intent;
import android.provider.Settings;

import com.lkm.app_ui.AppConfig;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.Market2_0ConfigeImpl;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppUIHelp;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.app_ui.help.RootCommand;
import com.lkm.app_ui.ui.fj.FjApp;
import com.lkm.comlib.fun_m.NetworkStateModule;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.P;
import com.lkm.frame.file.FileHelper;
import com.lkm.frame.module.BootTask;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.MainPresenter;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;
import com.lkm.market2_0.netapi.Api;
import com.lkm.market2_0.m_imple.steelyardserver.server.Config;
import com.lkm.usb_com.UsbModule;
import com.rockchip.rkapi.RKapi;
import com.ta.utdid2.device.UTDevice;

import java.io.File;


 
public class InitPreTask extends BootTask<Void, Void> {
    public InitPreTask() {
        super(new DefaultTaskExecutor(AppConst.getTaskExecutor(), false), InitPreTask.class.getName(), AppConst.getTaskManager2(), AppConst.getModuleManager());

    }

    @Override
    public boolean interceptPreExecute(Task<Void, ?, BootTaskResultWarp<Void>> task) {
        

        return super.interceptPreExecute(task);

    }



    public void postExecuting() {

        
        {
            AppConfig.DataFile.init();

            File file = new File(AppConfig.DataFile.getDeviceUncodePath());
            String deviceUncode = FileHelper.getString(file, AppConst.charset);

            if (StringHelp.isEmpty(deviceUncode)) {
                deviceUncode = UTDevice.getUtdid(AppConst.getApplication());
                FileHelper.SaveString(deviceUncode, file, AppConst.charset, true);
            }

            AppConst.deviceUncode = deviceUncode;
        }

        
        Config.upSteelyardStatusTime = 60*1000;

        P.isDebug = MyApplication.getInstance().isDebug();


//         /data/app/com.zhd-1.apk
        RootCommand.run("chmod 777 " + AppConst.getApplication().getPackageCodePath());

//            Settings.System.putInt(AppConst.getApplication().getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,15000);

//            Settings.System.putLong(AppConst.getApplication().getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,60000);
//            Settings.System.putInt(AppConst.getApplication().getContentResolver(),"lockscreen.disabled",false);



        
        Config.steelyardActionOutTime = 8 * 1000;

        initRunTimeData();

        if(RunTimeData.isXIANGCHENG){
            

            new Thread(){
                @Override
                public void run() {
                   String  s = RootCommand.runGetResult("wm size");
                   if(s!=null){
                       

                       if(s.contains("hysical size: 800x480")&&s.contains("verride size: 1024x600")){
                           RootCommand.run("wm size 800x480");
                           P.d("== 设置分辨回到800x480");
                       }
                   }
                }
            }.run();

        }


        initjava_market2_0();

        RunTimeData.setSZHost("");

        


        NetworkStateModule.getInstance().start(AppConst.getApplication());
        UsbModule.getInstance().init(AppConst.getApplication());
        
        UsbModule.getInstance().setAutoGrantedPermission(new Runnable() {
            @Override
            public void run() {
                UsbModule.autoGrantedPermission();
            }
        });


        new OnSteelyardInfo().start();

        FjApp.install.init();

        complete(new BootTaskResultWarp<>(true, null));
    }

    
    private void initjava_market2_0() {
        Market2_0ConfigeImpl market2_0Confige = new Market2_0ConfigeImpl();

        
        MainPresenter mMainPresenter = new MainPresenter(market2_0Confige);
        
        mMainPresenter.init();


        Api.init();
        String ip = market2_0Confige.getServerApiHost();
//      getSharedPreferences("settingapp",Context.MODE_PRIVATE).getString("tv_url", "http://192.168.1.172:8080/api/xx.jsp");
        Api.host = ip;

//      String szip = MyApplication.getInstance().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("tv_szurl", "");



        market2_0Confige.inServerSteelyardMac(AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("serverSteelyardMac", ""),
         AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("serverSteelyardMac_no", ""));
    }

    private void initRunTimeData(){
        try{
            RKapi.InitApi(AppConst.getApplication());
            RunTimeData.isXIANGCHENG = true;
        }catch (Error e){
            RunTimeData.isXIANGCHENG = false;
            e.printStackTrace();
        }

        RunTimeData.screenAutoOff = AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getInt("screenAutoOff", RunTimeData.screenAutoOff);

        RunTimeData.printerMode = AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("printerMode", RunTimeData.printerMode);

        RunTimeData.serialport = AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("serialport", RunTimeData.serialport);

//            RunTimeData.ast_oid = AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("ast_oid", "");

        RunTimeData.orderPrintTemplate = AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("orderPrintTemplate", RunTimeData.orderPrintTemplate);
//                FileHelper.SaveString(deviceUncode, file, AppConst.charset, true);


//        RunTimeData.LoaclSteelyardMac = AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("serverSteelyardMac", "");

        try {
            SteelyardValue steelyardValue = GsonHelp.fromJson(AppConst.getApplication().getSharedPreferences("netCache", Context.MODE_PRIVATE).getString("SteelyardValue",""), SteelyardValue.class);
            RunTimeData.oldSteelyardValue = steelyardValue;

        }catch (Exception e){}

    }
    @Override
    public void doExecuting(Void param) {
        super.doExecuting(param);
        
        mTaskExecutor.post(new Runnable() {
            @Override
            public void run() {
                postExecuting();
            }
        });

    }
    @Override
    protected void onDependencyModuleLoadFail() {

    }
}
